import { Injectable } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import { HttpClient, HttpParams } from '@angular/common/http';
import { BaseHttpService } from '../../../../shared/services/baseHttp.service';
import { CompanyGCSummaryModel } from './models/companyGCsummary.model';
import { CompanyGCPrincipleModel } from './models/companyGCprinciple.model';
import { CompanyGCImpactModel } from './models/companyGCimpact.model';
import { CompanyGCManagementModel } from './models/companyGCmanagement.model';
import { CompanyGCConclusionModel } from './models/companyGCconclusion.model';
import { ReplaySubject } from 'rxjs';
import { forkJoin } from 'rxjs/observable/forkJoin';

export enum GCAssesment {
    'Compliant' = 0,
    'Watchlist' = 1,
    'Non-Compliant' = 2
}

/**
 * Company global compact main service
 *
 * Author: Sorin Spiru
 * Date: 13.01.2018
 */
@Injectable()
export class CompanyGlobalCompactService extends BaseHttpService {

    private pricipleSubject = new ReplaySubject<any>(1);
    public principleData = this.pricipleSubject.asObservable();


    constructor(public http: HttpClient) {
        super();
    }

    /**
     * Get the company's global compact summary
     * @param companyId Company unique identifer
     * @param overallAssessment Required global compact assessment (optional)
     */
    public getByCompany(companyId: string, overallAssessment?: GCAssesment, fetchCount?: boolean): Observable<CompanyGCSummaryModel> {
        let params: HttpParams = new HttpParams();
        params = params.set('companyId', companyId)

        if(overallAssessment)
            params = params.set('overallAssessment', overallAssessment.toString());
        if(fetchCount)
            params = params.set('fetchCount', fetchCount.toString());

        return this.http.get<CompanyGCSummaryModel>(this.serverUrl + 'gccsserv/api/v1/gcoverallcompanysummary', { params: params });
    }

    /**
     * Get a company's global compact principles. Can return all of them, all by assessment or count.
     * @param companyId Company unique identifier
     * @param overallAssessment Principle assessment
     * @param fetchCount True to get count and false to get list
     */
    public getPrincipleSummaries(companyId: string, overallAssessment: GCAssesment, fetchCount: boolean): Observable<CompanyGCPrincipleModel[]>{
        let params: HttpParams = new HttpParams();
        params = params.set('companyId', companyId)
        .set('overallAssessment', overallAssessment.toString())
        .set('fetchCount', fetchCount.toString());

        return this.http.get<CompanyGCPrincipleModel[]>(this.serverUrl + 'gccsserv/api/v1/gcprinciplesummary', { params: params });
    }

    /**
     * Get impact of principle related event indicators
     * @param principleId Principle's unique identifier
     */
    public getPrincipleIndicatorImpact(principleId: string): Observable<CompanyGCImpactModel[]> {
        let params: HttpParams = new HttpParams();
        params = params.set('principleSummaryId', principleId);

        return this.http.get<CompanyGCImpactModel[]>(this.serverUrl + 'gccsserv/api/v1/gcprincipleeventindicatorimpact', { params: params });
    }

    /**
     * Get management information related to provided principle
     * @param principleId Principle's unique identifier
     */
    public getPrincipleIndicatorManagement(principleId: string): Observable<CompanyGCManagementModel[]> {
        let params: HttpParams = new HttpParams();
        params = params.set('principleSummaryId', principleId);

        return this.http.get<CompanyGCManagementModel[]>(this.serverUrl + 'gccsserv/api/v1/gcprincipleeventindicatormanagement', { params: params });
    }

    /**
     * Get conculsions of provided principle
     * @param principleId Principle's unique identifier
     */
    public getPrincipleConclusion(principleId: string): Observable<CompanyGCConclusionModel>{
        let params: HttpParams = new HttpParams();
        params = params.set('principleSummaryId', principleId);

        return this.http.get<CompanyGCConclusionModel>(this.serverUrl + 'gccsserv/api/v1/gcprincipleconclusion', { params: params });
    }

    public getOverallPricipleData( companyId: string ): Observable<any> {

        forkJoin([
            this.getPrincipleSummaries(companyId, GCAssesment['Non-Compliant'], false)
                .catch(() => Observable.of(false)), // return false if error (or 401)
            this.getPrincipleSummaries(companyId, GCAssesment.Watchlist, false)
                .catch(() => Observable.of(false)), // return false if error (or 401)
        ]).subscribe(results => {
            this.pricipleSubject.next(results);
        });

        return this.pricipleSubject;
    }

    public getPillars() {
        return [{
            id: 1,
            name: 'Human Rights',
            principles: [
                { id: 1, description: 'Businesses should support and respect the protection of internationally proclaimed human rights' },
                { id: 2, description: 'Businesses should make sure that they are not complicit in human rights abuses' }
            ]
        }, {
            id: 2,
            name: 'Labour Rights',
            principles: [
                // tslint:disable-next-line:max-line-length
                { id: 3, description: 'Businesses should uphold the freedom of association and the effective recognition of the right to collective bargaining' },
                { id: 4, description: 'Businesses should uphold the elimination of all forms of forced and compulsory labour' },
                { id: 5, description: 'Businesses should uphold the effective abolition of child labour' },
                { id: 6, description: 'Businesses should uphold the elimination of discrimination in respect of employment and occupation' }
            ]
        }, {
            id: 3,
            name: 'Environment',
            principles: [
                { id: 7, description: 'Businesses should support a precautionary approach to environmental challenges' },
                { id: 8, description: 'Businesses should undertake initiatives to promote greater environmental responsibility' },
                { id: 9, description: 'Businesses should encourage the development and diffusion of environmentally friendly technologies' }
            ]
        }, {
            id: 4,
            name: 'Business Ethics',
            principles: [
                { id: 10, description: 'Businesses should work against corruption in all its forms, including extortion and bribery' }
            ]
        }];
    }
}
